/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.common.web.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.WebView;
import com.qihoo.gamecenter.sdk.common.i.c;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class WebViewEx extends WebView {
    private static final String[] a = { "getClass", "hashCode", "notify", "notifyAll", "equals", "toString", "wait" };
    private HashMap b = new HashMap();
    private String c = null;

    public WebViewEx(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        a(paramContext);
    }

    public WebViewEx(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        a(paramContext);
    }

    public WebViewEx(Context paramContext) {
        super(paramContext);
        a(paramContext);
    }

    private void a(Context paramContext) {
        a();
    }

    public void loadUrl(String paramString) {
        if (!(TextUtils.isEmpty(paramString))) {
            String str = paramString.substring(0, paramString.indexOf(":"));
            if (str.equalsIgnoreCase("intent"))
                return;
        }
        super.loadUrl(paramString);
    }

    public void addJavascriptInterface(Object paramObject, String paramString) {
        if (TextUtils.isEmpty(paramString))
            return;
        if (f())
            super.addJavascriptInterface(paramObject, paramString);
        else
            this.b.put(paramString, paramObject);
    }

    @SuppressLint({ "NewApi" })
    public void removeJavascriptInterface(String paramString) {
        if (f()) {
            super.removeJavascriptInterface(paramString);
        }
        else {
            this.b.remove(paramString);
            this.c = null;
            b();
        }
    }

    @SuppressLint({ "NewApi" })
    private boolean a() {
        if ((e()) && (!(f()))) {
            super.removeJavascriptInterface("searchBoxJavaBridge_");
            return true;
        }
        return false;
    }

    private void b() {
        if (!(TextUtils.isEmpty(this.c))) {
            c();
            return;
        }
        String str = d();
        this.c = str;
        c();
    }

    private void c() {
        loadUrl(this.c);
    }

    private String d() {
        if (this.b.size() == 0) {
            this.c = null;
            return null;
        }
        Iterator localIterator = this.b.entrySet().iterator();
        StringBuilder localStringBuilder = new StringBuilder();
        localStringBuilder.append("javascript:(function JsAddJavascriptInterface_(){");
        try {
            while (localIterator.hasNext()) {
                Map.Entry localEntry = (Map.Entry) localIterator.next();
                String str = (String) localEntry.getKey();
                Object localObject = localEntry.getValue();
                a(str, localObject, localStringBuilder);
            }
        } catch (Exception localException) {
            c.b("WebViewEx", new Object[] { localException.toString() });
        }
        localStringBuilder.append("})()");
        return localStringBuilder.toString();
    }

    private void a(String paramString, Object paramObject, StringBuilder paramStringBuilder) {
        if ((TextUtils.isEmpty(paramString)) || (null == paramObject) || (null == paramStringBuilder))
            return;
        Class localClass = paramObject.getClass();
        paramStringBuilder.append("if(typeof(window.").append(paramString).append(")!='undefined'){");
        paramStringBuilder.append("    console.log('window." + paramString + "_js_interface_name is exist!!');");
        paramStringBuilder.append("}else {");
        paramStringBuilder.append("    window.").append(paramString).append("={");
        Method[] arrayOfMethod1 = localClass.getMethods();
        for (Method localMethod : arrayOfMethod1) {
            String str = localMethod.getName();
            if (a(str))
                continue;
            paramStringBuilder.append("        ").append(str).append(":function(");
            int k = localMethod.getParameterTypes().length;
            int l;
            int i1;
            if (k > 0) {
                l = k - 1;
                for (i1 = 0; i1 < l; ++i1)
                    paramStringBuilder.append("arg").append(i1).append(",");
                paramStringBuilder.append("arg").append(k - 1);
            }
            paramStringBuilder.append(") {");
            if (localMethod.getReturnType() != Void.TYPE)
                paramStringBuilder.append("            return ").append("prompt('").append("MyApp:").append("'+");
            else
                paramStringBuilder.append("            prompt('").append("MyApp:").append("'+");
            paramStringBuilder.append("JSON.stringify({");
            paramStringBuilder.append("obj").append(":'").append(paramString).append("',");
            paramStringBuilder.append("func").append(":'").append(str).append("',");
            paramStringBuilder.append("args").append(":[");
            if (k > 0) {
                l = k - 1;
                for (i1 = 0; i1 < l; ++i1)
                    paramStringBuilder.append("arg").append(i1).append(",");
                paramStringBuilder.append("arg").append(l);
            }
            paramStringBuilder.append("]})");
            paramStringBuilder.append(");");
            paramStringBuilder.append("        }, ");
        }
        paramStringBuilder.append("    };");
        paramStringBuilder.append("}");
    }

    private boolean a(String paramString) {
        for (String str : a)
            if (str.equals(paramString))
                return true;
        return false;
    }

    private boolean e() {
        return (Build.VERSION.SDK_INT >= 11);
    }

    private boolean f() {
        return (Build.VERSION.SDK_INT >= 17);
    }
}